import operate from '@dfeidao/atom-nodejs/db/operate';
import log from '@dfeidao/atom-nodejs/logger/log';
import doc from "@dfeidao/atom-nodejs/msg/doc-code";
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';

interface Message {
	student_no: string;
	productid: string;
	teacher_no: string;
	teacher_name: string;
	student_name: string;
	parent_number: string;
	parent_name: string;
	arrive_time: number;
	remaining_class_hours: string;
	hours_used: string;
	class_id: string;
	class_name: string;
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-manual-check/s004,action_id:' + action_id);
	const r = await doc(action_id, session_id, systemid, "BM0016033", "", 1);
	const { insert, exec } = operate(action_id, session_id, systemid);
	insert('check_in', { sign_in_number: r.result, teacher_no: message.teacher_no, teacher_name: message.teacher_name, student_name: message.student_name, parent_number: message.parent_number, parent_name: message.parent_name, productid: message.productid, student_no: message.student_no, arrive_time: message.arrive_time, check_in_status: "1", remaining_class_hours: message.remaining_class_hours, hours_used: message.hours_used, class_id: message.class_id });
	// student_no;
	// student_name;
	// class_id;
	// class_name;
	// teacher_no;
	// teacher_name;
	// supplementary_content;
	// supplementary_time;
	// status;
	// recording_time;
	// productid;
	const date = sys_date(action_id, session_id);
	const { result: check_attendance_info_num } = await doc(action_id, session_id, systemid, 'BM0016024', '', 1);
	insert('check_attendance_info', {// 考勤记录>表
		check_attendance_info_num,
		teacher_no: message.teacher_no,
		teacher_name: message.teacher_name,
		class_id: message.class_id,
		class_name: message.class_name,
		student_name: message.student_name,
		student_no: message.student_no,
		productid: message.productid,
		record_time: date,
		status: '4'
	});
	const ret = await exec();
	log('Service end path:app-manual-check/s004,action_id:' + action_id);
	return { data: ret };
	// if (ret.code === 1) {
	// 	return { data: "success" };
	// } else {
	// 	return { data: "fail" };
	// }
}
